package com.runa.rccprotocol.dto.pusai;

import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.runa.RunaConcenRequestData;
import lombok.Data;

/**
 * 2024/12/3 9:46
 * zhangYB
 */
@Data
public class PsValveRequestData {


    public byte[] readValveAddress() {
        byte[] requestData = new byte[100];
        int index = 0;
//        requestData[index++] = (byte)0xFE;
//        requestData[index++] = (byte)0xFE;
//        requestData[index++] = (byte)0xFE;
        int begin = index;
        requestData[index++] = 0x68;
        requestData[index++] = (byte)0x41;

        for (int i = 0; i < 7; i++) {
            requestData[index++] = (byte)0xAA;
        }

        requestData[index++] = 0x03;
        requestData[index++] = 0x03;
        requestData[index++] = (byte)0x0A;
        requestData[index++] = (byte)0x81;
        requestData[index++] = 0x00;
        requestData[index++] = ByteUtil.sum(requestData, begin, index - begin);
        requestData[index++] = 0x16;

        byte[] r = new byte[index];
        System.arraycopy(requestData, 0, r, 0, r.length);
        return r;
    }

    public byte[] readValve(String serial) {
        while (serial.length() < 12) {
            serial = "0" + serial;
        }
        if (serial.length() == 12) {
            serial = "10" + serial;
        }
        while (serial.length() < 14) {
            serial = "0" + serial;
        }
        byte[] requestData = new byte[100];
        int index = 0;
//        requestData[index++] = (byte)0xFE;
//        requestData[index++] = (byte)0xFE;
//        requestData[index++] = (byte)0xFE;
        int begin = index;
        requestData[index++] = 0x68;
        requestData[index++] = (byte)0x41;

        byte[] bytes = ByteUtil.hexStringToByteFlip(serial);
        for (int i = 0; i < bytes.length; i++) {
            requestData[index++] = bytes[i];
        }
        requestData[index++] = 0x01;
        requestData[index++] = 0x03;
        requestData[index++] = (byte)0x2D;
        requestData[index++] = (byte)0x90;
        requestData[index++] = 0x00;
        requestData[index++] = ByteUtil.sum(requestData, begin, index - begin);
        requestData[index++] = 0x16;

        byte[] r = new byte[index];
        System.arraycopy(requestData, 0, r, 0, r.length);
        return r;
    }

    public byte[] setValveMaxOpen(String serial, Integer opening) {
        opening = opening == null ? 0 : opening;
        serial = handleSerial(serial);
        byte[] requestData = new byte[100];
        int index = 0;
        int begin = index;
        requestData[index++] = 0x68;
        requestData[index++] = (byte)0x41;

        byte[] bytes = ByteUtil.hexStringToByteFlip(serial);
        for (int i = 0; i < bytes.length; i++) {
            requestData[index++] = bytes[i];
        }
        requestData[index++] = 0x04;
        requestData[index++] = 0x05;
        requestData[index++] = (byte)0x17;
        requestData[index++] = (byte)0xA0;
        requestData[index++] = 0x00;
        requestData[index++] = 0x56;
        requestData[index++] = (byte)(opening & 0xff);

        requestData[index++] = ByteUtil.sum(requestData, begin, index - begin);
        requestData[index++] = 0x16;

        byte[] r = new byte[index];
        System.arraycopy(requestData, 0, r, 0, r.length);
        return r;
    }

    public byte[] setValveMinOpen(String serial, Integer opening) {
        opening = opening == null ? 0 : opening;
        serial = handleSerial(serial);
        byte[] requestData = new byte[100];
        int index = 0;
        int begin = index;
        requestData[index++] = 0x68;
        requestData[index++] = (byte)0x41;

        byte[] bytes = ByteUtil.hexStringToByteFlip(serial);
        for (int i = 0; i < bytes.length; i++) {
            requestData[index++] = bytes[i];
        }
        requestData[index++] = 0x04;
        requestData[index++] = 0x04;
        requestData[index++] = (byte)0x17;
        requestData[index++] = (byte)0xA0;
        requestData[index++] = 0x00;
        requestData[index++] = 0x58;
        requestData[index++] = (byte)(opening & 0xff);
        requestData[index++] = ByteUtil.sum(requestData, begin, index - begin);
        requestData[index++] = 0x16;

        byte[] r = new byte[index];
        System.arraycopy(requestData, 0, r, 0, r.length);
        return r;
    }

    public String handleSerial(String serial) {
        while (serial.length() < 12) {
            serial = "0" + serial;
        }
        if (serial.length() == 12) {
            serial = "10" + serial;
        }
        return serial;
    }


    public byte[] setValveOpen(String serial, Integer opening, boolean ifLock) {
        opening = opening == null ? 0 : opening;
        byte valveStatus;
        if (ifLock) {
            // 强制开关阀
            valveStatus = opening == 0 ? (byte)0x99 : (byte)0x33;
        } else {
            valveStatus = opening == 0 ? (byte)0xBB : (byte)0x55;
        }
        serial = handleSerial(serial);
        byte[] requestData = new byte[100];
        int index = 0;
        int begin = index;
        requestData[index++] = 0x68;
        requestData[index++] = (byte)0x41;

        byte[] bytes = ByteUtil.hexStringToByteFlip(serial);
        for (int i = 0; i < bytes.length; i++) {
            requestData[index++] = bytes[i];
        }
        requestData[index++] = 0x04;
        requestData[index++] = 0x04;
        requestData[index++] = (byte)0x17;
        requestData[index++] = (byte)0xA0;
        requestData[index++] = 0x00;
        requestData[index++] = valveStatus;
        requestData[index++] = ByteUtil.sum(requestData, begin, index - begin);
        requestData[index++] = 0x16;

        byte[] r = new byte[index];
        System.arraycopy(requestData, 0, r, 0, r.length);
        return r;


    }


    public static void main(String[] args) {
        PsValveRequestData psValveRequestData = new PsValveRequestData();
        byte[] bytes = psValveRequestData.readValve("202400119765");
        System.out.println("天罡透传-抄阀: " + ByteUtil.byteToHexString(bytes));
        bytes = psValveRequestData.setValveOpen("202400119765", 100, true);
        System.out.println("天罡透传-开阀: " + ByteUtil.byteToHexString(bytes));
        bytes = psValveRequestData.setValveOpen("202400119765", 0, true);
        System.out.println("天罡透传-关阀: " + ByteUtil.byteToHexString(bytes));
        bytes = psValveRequestData.setValveMaxOpen("202400119765", 50);
        System.out.println("天罡透传-设置最大开度: " + ByteUtil.byteToHexString(bytes));


        RunaConcenRequestData requestData = new RunaConcenRequestData("244800508");
        requestData.readPsValve("202400119570", 4);
        System.out.println("瑞纳透传-抄阀: " + ByteUtil.byteToHexString(requestData.getData()));


    }



}
